package hl.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import hl.dao.BaseDao;
import hl.model.TCoach;
import hl.model.TVenueInfo;
import hl.pageModel.Coach;
import hl.pageModel.DataGrid;
import hl.service.CoachService;

@Service("coachService")
public class CoachServiceImpl implements CoachService {

	private BaseDao<TCoach> coachDao;

	private BaseDao<TVenueInfo> venueInfoDao;

	public BaseDao<TVenueInfo> getVenueInfoDao() {
		return venueInfoDao;
	}

	@Autowired
	public void setVenueInfoDao(BaseDao<TVenueInfo> venueInfoDao) {
		this.venueInfoDao = venueInfoDao;
	}

	public BaseDao<TCoach> getcoachDao() {
		return coachDao;
	}

	@Autowired
	public void setcoachDao(BaseDao<TCoach> coachDao) {
		this.coachDao = coachDao;
	}


	@Override
	public DataGrid getDatagrid(Coach coach) {
		DataGrid dg = new DataGrid();
		String hql = "from TCoach t ";

		Map<String, Object> params = new HashMap<String, Object>();

		hql = search(coach, hql, params);

		if (coach.getOrder() != null) {
			hql += " order by " + coach.getSort() + " " + coach.getOrder();
		}

		List<TCoach> l = coachDao.find(hql, params, coach.getPage(), coach.getRows());

		int totalHql = coachDao.count(hql, params);

		List<Coach> pl = new ArrayList();
		if (l != null && l.size() > 0) {
			for (TCoach t : l) {
				Coach u = new Coach();
				BeanUtils.copyProperties(t, u);
				u.setVenueName(t.getTVenueInfo().getName());
				u.setPkVenueInfo(t.getTVenueInfo().getPkVenueInfo());
				pl.add(u);
			}
		}
		dg.setRows(pl);
		dg.setTotal(totalHql);
		return dg;
	}

	private String search(Coach coach, String hql, Map<String, Object> params) {
		if (coach.getName() != null && !coach.getName().trim().equals("")) {
			hql += " where t.name like :value1 ";
			params.put("value1", "%" + coach.getName().trim() + "%");
		}

		if (coach.getGender() != null && !coach.getGender().equals("")) {
			if (hql.contains("where"))
				hql += " and ";
			else
				hql += " where ";
			hql += " t.gender like :value2 ";
			params.put("value2", "%" + coach.getGender().trim() + "%");
		}
		if (coach.getPkCoach() != null && !coach.getPkCoach().equals("")) {
			if (hql.contains("where"))
				hql += " and ";
			else
				hql += " where ";
			hql += " t.pkcoach like :value3 ";
			params.put("value3", "%" + coach.getPkCoach().toString() + "%");
		}
		return hql;
	}

	@Override
	public void save(Coach coach) {
		TCoach t = new TCoach();
		BeanUtils.copyProperties(coach, t);
		t.setCdatetime(new Date());
		t.setTVenueInfo(venueInfoDao.get(TVenueInfo.class, coach.getPkVenueInfo()));
		coachDao.save(t);
	}

	@Override
	public void edit(Coach coach) {
		TCoach t = new TCoach();
		BeanUtils.copyProperties(coach, t);
		t.setMdatetime(new Date());
		t.setTVenueInfo(venueInfoDao.get(TVenueInfo.class, coach.getPkVenueInfo()));
		coachDao.saveOrUpdate(t);
	}

	@Override
	public void remove(Coach coach) {
		TCoach t = coachDao.get(TCoach.class, coach.getPkCoach());
		coachDao.delete(t);
	}

	@Override
	public Object getComboBox() {
		List<TCoach> l=coachDao.find("from TCoach t where t.workStatus=?", new Object[] {"在职"});
		List<Coach> pl = new ArrayList();
		if (l != null && l.size() > 0) {
			for (TCoach t : l) {
				Coach u = new Coach();
				BeanUtils.copyProperties(t, u);
				u.setVenueName(t.getTVenueInfo().getName());
				u.setPkVenueInfo(t.getTVenueInfo().getPkVenueInfo());
				pl.add(u);
			}
		}
		return pl;
	}
}
